package com.sgb.kjwl.view.ui.adapter.lease;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemGaodeSearchBinding;
import com.sgb.kjwl.model.LocationAddressInfo;

import java.util.ArrayList;

/**
 * 租赁首页高德地图搜索关键字列表适配器
 */
public class GaodeSearchAdapter extends RecyclerView.Adapter<GaodeSearchAdapter.ViewHolder> {
    private Context context;
    private ArrayList<LocationAddressInfo> datas;
    private final LayoutInflater inflater;
    private Callback callback;


    public void setCallback(Callback callback) {
        this.callback = callback;
    }


    public GaodeSearchAdapter(Context context, ArrayList<LocationAddressInfo> list) {
        this.context = context;
        datas = list;
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemGaodeSearchBinding binding = DataBindingUtil.inflate(inflater, R.layout.item_gaode_search, parent, false);
        return new ViewHolder(binding);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        LocationAddressInfo data = datas.get(position);
        if (data.getTip() == 2) {
            holder.getBinding().icon.setImageResource(R.mipmap.icon_map_1);
            holder.getBinding().name.setText("图选");
        } else {
            if (data.getTip() == 1) {
                holder.getBinding().icon.setImageResource(R.mipmap.time_gray_icon);
                holder.getBinding().name.setText(data.getTitle());
            } else {
                holder.getBinding().icon.setImageResource(R.mipmap.icon_dingwei);
                SpannableStringBuilder builder = new SpannableStringBuilder(data.getTitle());
                ForegroundColorSpan buleSpan = new ForegroundColorSpan(Color.parseColor("#5792FD"));
                int len = data.getWork().length();
                int n = data.getTitle().indexOf(data.getWork());
                if (n != -1) {
                    builder.setSpan(buleSpan, n, n + len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                holder.getBinding().name.setText(builder);
            }
        }
        holder.getBinding().tvDetail.setText(data.getText());
        holder.itemView.setOnClickListener(v -> callback.back(data));
        holder.getBinding().executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ItemGaodeSearchBinding mBinding;

        public ItemGaodeSearchBinding getBinding() {
            return mBinding;
        }

        public ViewHolder(ItemGaodeSearchBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }
    }

    public interface Callback {
        void back(LocationAddressInfo bean);

    }

}